package com.zdp.leetcodeMiddle;

import java.util.LinkedList;
import java.util.Queue;

/*
* 题目描述：
* 给你一个由 '1'（陆地）和 '0'（水）组成的的二维网格，请你计算网格中岛屿的数量。
岛屿总是被水包围，并且每座岛屿只能由水平方向和/或竖直方向上相邻的陆地连接形成。
此外，你可以假设该网格的四条边均被水包围。
示例 1：
输入：grid = [
  ["1","1","1","1","0"],
  ["1","1","0","1","0"],
  ["1","1","0","0","0"],
  ["0","0","0","0","0"]
]
输出：1
示例 2：
输入：grid = [
  ["1","1","0","0","0"],
  ["1","1","0","0","0"],
  ["0","0","1","0","0"],
  ["0","0","0","1","1"]
]
输出：3
提示：
m == grid.length
n == grid[i].length
1 <= m, n <= 300
grid[i][j] 的值为 '0' 或 '1'
来源：力扣（LeetCode）
链接：https://leetcode-cn.com/problems/number-of-islands
著作权归领扣网络所有。商业转载请联系官方授权，非商业转载请注明出处。
* */
public class 岛屿数量_200 {
    public static void main(String[] args) {
        岛屿数量_200 demo  = new 岛屿数量_200();
        char[][] grid = new char[][]{
                {
                    '1','1','0','0','0'
                },
                {
                    '1','1','0','0','0'
                },
                {
                    '0','0','1','0','0'
                },
                {
                    '0','0','0','1','1'
                }
        };
        int i = demo.numIslands(grid);
        System.out.println(i);
    }
    /*
    * 题解：跟单词搜索类似，就是利用深搜
    * 就是找到一个'1'的所有相邻的'1' 直到找不到了 这就是1块岛屿
    * 不同点在于 不需要将标记数组重置  走过的1 不能重复走
    * 就是深度优先搜索
    * */
    public int numIslands(char[][] grid) {
        int[][] visited = new int[grid.length][grid[0].length];
        int num = 0;
        for(int i=0;i<grid.length;i++){
            for(int j = 0 ; j < grid[0].length; j ++ ){
                if(grid[i][j] == '1' && visited[i][j] == 0){
                    visited[i][j] = 1;
                    f(grid,visited,i,j);
                    num ++;
                }
            }
        }
        return num;
    }
    public void f(char[][] grid,int[][] visited,int i,int j){
        // 上
        if(i-1>=0&&grid[i-1][j]=='1'&&visited[i-1][j]==0){
            visited[i-1][j] = 1;
            f(grid,visited,i-1,j);
        }
        // 下
        if(i+1<grid.length&&grid[i+1][j]=='1'&&visited[i+1][j]==0){
            visited[i+1][j] = 1;
            f(grid,visited,i+1,j);
        }
        // 左
        if(j-1>=0&&grid[i][j-1]=='1'&&visited[i][j-1]==0){
            visited[i][j-1] = 1;
            f(grid,visited,i,j-1);
        }
        // 右
        if(j+1<grid[0].length&&grid[i][j+1]=='1'&&visited[i][j+1]==0){
            visited[i][j+1] = 1;
            f(grid,visited,i,j+1);
        }
    }

    /*
    * 官方题解：深搜，广搜，并查集
    * */
    /*
    * 深搜
    * */
    void dfs(char[][] grid, int r, int c) {
        int nr = grid.length;
        int nc = grid[0].length;

        if (r < 0 || c < 0 || r >= nr || c >= nc || grid[r][c] == '0') {
            return;
        }

        grid[r][c] = '0';
        dfs(grid, r - 1, c);
        dfs(grid, r + 1, c);
        dfs(grid, r, c - 1);
        dfs(grid, r, c + 1);
    }
    public int numIslands1(char[][] grid) {
        if (grid == null || grid.length == 0) {
            return 0;
        }

        int nr = grid.length;
        int nc = grid[0].length;
        int num_islands = 0;
        for (int r = 0; r < nr; ++r) {
            for (int c = 0; c < nc; ++c) {
                if (grid[r][c] == '1') {
                    ++num_islands;
                    dfs(grid, r, c);
                }
            }
        }

        return num_islands;
    }

    /*
    * 广搜，将位置为1的加入队列
    * */
    public int numIslands2(char[][] grid) {
        if (grid == null || grid.length == 0) {
            return 0;
        }

        int nr = grid.length;
        int nc = grid[0].length;
        int num_islands = 0;

        for (int r = 0; r < nr; ++r) {
            for (int c = 0; c < nc; ++c) {
                if (grid[r][c] == '1') {
                    ++num_islands;
                    grid[r][c] = '0';
                    Queue<Integer> neighbors = new LinkedList<>();
                    neighbors.add(r * nc + c);
                    while (!neighbors.isEmpty()) {
                        int id = neighbors.remove();
                        int row = id / nc;
                        int col = id % nc;
                        if (row - 1 >= 0 && grid[row-1][col] == '1') {
                            neighbors.add((row-1) * nc + col);
                            grid[row-1][col] = '0';
                        }
                        if (row + 1 < nr && grid[row+1][col] == '1') {
                            neighbors.add((row+1) * nc + col);
                            grid[row+1][col] = '0';
                        }
                        if (col - 1 >= 0 && grid[row][col-1] == '1') {
                            neighbors.add(row * nc + col-1);
                            grid[row][col-1] = '0';
                        }
                        if (col + 1 < nc && grid[row][col+1] == '1') {
                            neighbors.add(row * nc + col+1);
                            grid[row][col+1] = '0';
                        }
                    }
                }
            }
        }

        return num_islands;
    }
    /*
    * 并查集
    * 为了求出岛屿的数量，我们可以扫描整个二维网格。如果一个位置为 11，
    * 则将其与相邻四个方向上的 11 在并查集中进行合并。
        最终岛屿的数量就是并查集中连通分量的数目。
    * */
    // 并查集
    class UnionFind {
        int count;
        int[] parent;
        int[] rank;
        public UnionFind(char[][] grid) {
            count = 0;
            int m = grid.length;
            int n = grid[0].length;
            parent = new int[m * n];
            rank = new int[m * n];
            for (int i = 0; i < m; ++i) {
                for (int j = 0; j < n; ++j) {
                    if (grid[i][j] == '1') {
                        parent[i * n + j] = i * n + j;
                        ++count;
                    }
                    rank[i * n + j] = 0;
                }
            }
        }

        public int find(int i) {
            if (parent[i] != i) parent[i] = find(parent[i]);
            return parent[i];
        }

        public void union(int x, int y) {
            int rootx = find(x);
            int rooty = find(y);
            if (rootx != rooty) {
                if (rank[rootx] > rank[rooty]) {
                    parent[rooty] = rootx;
                } else if (rank[rootx] < rank[rooty]) {
                    parent[rootx] = rooty;
                } else {
                    parent[rooty] = rootx;
                    rank[rootx] += 1;
                }
                --count;
            }
        }

        public int getCount() {
            return count;
        }
    }

    public int numIslands3(char[][] grid) {
        if (grid == null || grid.length == 0) {
            return 0;
        }

        int nr = grid.length;
        int nc = grid[0].length;
        int num_islands = 0;
        UnionFind uf = new UnionFind(grid);
        for (int r = 0; r < nr; ++r) {
            for (int c = 0; c < nc; ++c) {
                if (grid[r][c] == '1') {
                    grid[r][c] = '0';
                    if (r - 1 >= 0 && grid[r-1][c] == '1') {
                        uf.union(r * nc + c, (r-1) * nc + c);
                    }
                    if (r + 1 < nr && grid[r+1][c] == '1') {
                        uf.union(r * nc + c, (r+1) * nc + c);
                    }
                    if (c - 1 >= 0 && grid[r][c-1] == '1') {
                        uf.union(r * nc + c, r * nc + c - 1);
                    }
                    if (c + 1 < nc && grid[r][c+1] == '1') {
                        uf.union(r * nc + c, r * nc + c + 1);
                    }
                }
            }
        }

        return uf.getCount();
    }


}
